﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UIDATALayer;
namespace UserData
{
    public partial class Control_StationState : UserControl,Interface_UserControl
    {
        string strName = "Sensor码";
        [Category("站位名称")]
        public string 站位名称
        {
            set { strName = value; }
            get { return strName; }
        }
        [Category("状态字体")]
        public Font 字体
        {
            set { lbl_StationResult.Font = value; }
            get { return lbl_StationResult.Font; }
        }

        public Control_StationState()
        {
            InitializeComponent();
        }
        private UIDATALayer.UIDataAction<StationStateData> acStationStateData = null;

        private void Control_StationState_Load(object sender, EventArgs e)
        {
            

        }
        public void Init()
        {
            StationStateHelper.OnTest_ShowStep += ShowStationResult;
        }
        public void ShowStationResult(string stationName, string value, LogType logType)
        {
            if(acStationStateData==null)
            {
                UIDATALayerHelper.RegistAction<StationStateData>(nameof(StationStateData), ShowStationResult).Start();
                acStationStateData = UIDATALayer.UIDATALayerHelper.GetRegistAction<StationStateData>(nameof(StationStateData));
            }
            if (stationName == this.strName)
            {
                StationStateData data = new StationStateData();
                data.stationName = strName;
                data.value = value;
                data.logType = logType;
                acStationStateData.SetData(data);

            }
        }

        public void ShowStationResult(StationStateData data)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action(() => { ShowStationResult(data); }));
            }
            else
            {
                if (data.stationName != strName)
                    return;
                if (data.logType == LogType.Err)
                {
                    lbl_StationResult.BackColor = Color.Red;
                }
                else if (data.logType == LogType.Info)
                {
                    lbl_StationResult.BackColor = Color.Green;
                }
                else
                {
                    lbl_StationResult.BackColor = Color.Yellow;
                }
                lbl_StationResult.Text = data.value;

            }
        }
    }
}
